using System;
using System.Windows.Media;
namespace System.Windows.Interactivity
{
	public static class Interaction
	{
		public static readonly DependencyProperty TriggersProperty = DependencyProperty.RegisterAttached("Triggers", typeof(TriggerCollection), typeof(Interaction), new PropertyMetadata(new PropertyChangedCallback(Interaction.OnTriggersChanged)));
		public static readonly DependencyProperty BehaviorsProperty = DependencyProperty.RegisterAttached("Behaviors", typeof(BehaviorCollection), typeof(Interaction), new PropertyMetadata(new PropertyChangedCallback(Interaction.OnBehaviorsChanged)));
		public static TriggerCollection GetTriggers(DependencyObject obj)
		{
			TriggerCollection triggerCollection = (TriggerCollection)obj.GetValue(Interaction.TriggersProperty);
			if (triggerCollection == null)
			{
				triggerCollection = new TriggerCollection();
				obj.SetValue(Interaction.TriggersProperty, triggerCollection);
			}
			return triggerCollection;
		}
		public static BehaviorCollection GetBehaviors(DependencyObject obj)
		{
			BehaviorCollection behaviorCollection = (BehaviorCollection)obj.GetValue(Interaction.BehaviorsProperty);
			if (behaviorCollection == null)
			{
				behaviorCollection = new BehaviorCollection();
				obj.SetValue(Interaction.BehaviorsProperty, behaviorCollection);
			}
			return behaviorCollection;
		}
		private static void OnBehaviorsChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
		{
			BehaviorCollection behaviorCollection = (BehaviorCollection)args.get_OldValue();
			BehaviorCollection behaviorCollection2 = (BehaviorCollection)args.get_NewValue();
			if (behaviorCollection != behaviorCollection2)
			{
				if (behaviorCollection != null && ((IAttachedObject)behaviorCollection).AssociatedObject != null)
				{
					behaviorCollection.Detach();
				}
				if (behaviorCollection2 != null && obj != null)
				{
					if (((IAttachedObject)behaviorCollection2).AssociatedObject != null)
					{
						throw new InvalidOperationException(ExceptionStringTable.CannotHostBehaviorCollectionMultipleTimesExceptionMessage);
					}
					behaviorCollection2.Attach(obj);
				}
			}
		}
		private static void OnTriggersChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
		{
			TriggerCollection triggerCollection = args.get_OldValue() as TriggerCollection;
			TriggerCollection triggerCollection2 = args.get_NewValue() as TriggerCollection;
			if (triggerCollection != triggerCollection2)
			{
				if (triggerCollection != null && ((IAttachedObject)triggerCollection).AssociatedObject != null)
				{
					triggerCollection.Detach();
				}
				if (triggerCollection2 != null && obj != null)
				{
					if (((IAttachedObject)triggerCollection2).AssociatedObject != null)
					{
						throw new InvalidOperationException(ExceptionStringTable.CannotHostTriggerCollectionMultipleTimesExceptionMessage);
					}
					triggerCollection2.Attach(obj);
				}
			}
		}
		internal static bool IsElementLoaded(FrameworkElement element)
		{
			UIElement rootVisual = Application.get_Current().get_RootVisual();
			DependencyObject parent = element.get_Parent();
			if (parent == null)
			{
				parent = VisualTreeHelper.GetParent(element);
			}
			return parent != null || (rootVisual != null && element == rootVisual);
		}
	}
}
